package jsyf.oa.service.EquipmentAccount;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.EquipmentAccount.EquipmentDao;
import jsyf.oa.dao.attach.AttachModuleDao;
import jsyf.oa.dao.device.DeviceDisplaceDao;
import jsyf.oa.dao.device.DeviceSampleDao;
import jsyf.oa.dao.wreport.WReportCusAndVisitDao;
import jsyf.oa.dao.wreport.WReportCusVisitDao;
import jsyf.oa.dao.wreport.WReportCustomerDao;
import jsyf.oa.entity.attach.AttachModuleEntity;
import jsyf.oa.entity.attach.AttachmentEntity;
import jsyf.oa.entity.device.DeviceDisplaceEntity;
import jsyf.oa.entity.device.DeviceSampleEntity;
import jsyf.oa.entity.device.DeviceSampleMainEntity;
import jsyf.oa.entity.equipmentAccount.DeviceInfoEntity;
import jsyf.oa.entity.equipmentAccount.EquipmentAccountEntity;
import jsyf.oa.entity.wreport.WReportCusAndVistEntity;
import jsyf.oa.entity.wreport.WReportCusVisitEntity;
import jsyf.oa.entity.wreport.WReportCustomerEntity;
import jsyf.oa.facade.equipmentAccount.EquipmentAccountFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class EquipmentService implements EquipmentAccountFacade {

    @Autowired
    private EquipmentDao equipmentDao;
    @Autowired
    private WReportCusVisitDao wReportCusVisitDao;
    @Autowired
    private WReportCusAndVisitDao wReportCusAndVisitDao;
    @Autowired
    private DeviceSampleDao deviceSampleDao;
    @Autowired
    private DeviceDisplaceDao deviceDisplaceDao;
    @Autowired
    private WReportCustomerDao wReportCustomerDao;
    @Autowired
    private AttachModuleDao attachModuleDao;
    @Override
    public JSONObject getDeviceList(String jsonData) {
        JSONObject json = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        int requestPage = 1;
        int pageSize = Integer.MAX_VALUE;
        StringBuffer condition=new StringBuffer();
        List<Object> sqlArgs = new ArrayList<Object>();
       if (!StringUtils.isBlank(json.getString("pageSize"))) {
            pageSize = json.getInteger("pageSize");
        }
        if (!StringUtils.isBlank(json.getString("requestPage"))) {
            requestPage = json.getInteger("requestPage");
        }
        if (!StringUtils.isBlank(json.getString("key")) ) {
            condition.append(" AND (t_con_main.BUYER LIKE ? ");
            sqlArgs.add("%" + json.getString("key") + "%");
            condition.append(" OR t_wrt_customer.TEL LIKE ? ");
            sqlArgs.add("%" + json.getString("key") + "%");
            condition.append(" OR v_dev_device.sequence LIKE ? ");
            sqlArgs.add("%" + json.getString("key") + "%");
            condition.append(" OR v_dev_device.brandName LIKE ? ");
            sqlArgs.add("%" + json.getString("key") + "%");
            condition.append(" OR v_dev_device.deviceTypeName LIKE ? ");
            sqlArgs.add("%" + json.getString("key") + "%");
            condition.append(" OR v_dev_device.modelName LIKE ? ");
            sqlArgs.add("%" + json.getString("key") + "%");
            condition.append(" OR t_con_main.SELLER LIKE ? ");
            sqlArgs.add("%" + json.getString("key") + "%");
            condition.append(" )");
        }
        if (!StringUtils.isBlank(json.getString("brandName")) ){
            condition.append(" AND v_dev_device.brandName =?");
            sqlArgs.add(json.getString("brandName"));
        }
        condition.append(" ORDER BY t_sys_company.SORT_NO,v_dev_device.brandName,v_dev_device.modelName");
        List<DeviceInfoEntity> recordsList = equipmentDao.getDeviceList(condition.toString(),sqlArgs.toArray(), requestPage, pageSize);
       for (int i=0;i<recordsList.size();i++){
           String sequence=recordsList.get(i).getSequence();
           List<WReportCusAndVistEntity> wReportCusAndVistEntity= wReportCusAndVisitDao.getByPage("  and serialNo=? and type=1 ORDER BY reportDate desc ",new Object[]{sequence},1,1);
           if (wReportCusAndVistEntity!=null&&wReportCusAndVistEntity.size()>0){
               recordsList.get(i).setRecentContactDate(wReportCusAndVistEntity.get(0).getReportDate());
               recordsList.get(i).setLatestWorkingHours(wReportCusAndVistEntity.get(0).getFwLatestWorkingHours());
           }
       }
       int recordCount = equipmentDao.getDeviceList(condition.toString(),sqlArgs.toArray(), 1, Integer.MAX_VALUE).size();
        //如果整除表示正好分N页，如果不能整除在N页的基础上+1页
        int pageCount = recordCount % pageSize == 0 ? recordCount / pageSize : (recordCount / pageSize) + 1;
        //存放分页信息
        JSONObject sendData = new JSONObject();
        //存放list
        JSONObject result = new JSONObject();
        sendData.put("recordCount", recordCount);
        sendData.put("pageCount", pageCount);
        sendData.put("requestPage", 1);
        result.put("pager", sendData);
        // 子数组对象
        result.put("records", recordsList);
        return result;
    }

    @Override
    public JSONObject getDeviceById(String jsonData) {
        JSONObject json = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        StringBuffer condition=new StringBuffer();
        List<Object> sqlArgs = new ArrayList<Object>();
        int requestPage = 1;
        int pageSize = Integer.MAX_VALUE;
        if (!StringUtils.isBlank(json.getString("id"))){
                condition.append(" and v_dev_device.id=?");
                sqlArgs.add(json.getString("id"));
            }
        EquipmentAccountEntity equipmentAccountEntity=new EquipmentAccountEntity();
        List<DeviceInfoEntity> recordsList = equipmentDao.getDeviceList(condition.toString(),sqlArgs.toArray(), requestPage, pageSize);
        if (recordsList!=null&&recordsList.size()>0){
            //基本信息
            DeviceInfoEntity deviceInfoEntity=  recordsList.get(0);
            String sequence= deviceInfoEntity.getSequence();
            if (deviceInfoEntity!=null) {
                equipmentAccountEntity.setDeviceInfo(deviceInfoEntity);
            }
            //服务记录
            List<WReportCusAndVistEntity> fwWReportCusVisitEntity=new ArrayList<>();
            if (!StringUtils.isBlank(sequence)){
                fwWReportCusVisitEntity=wReportCusAndVisitDao.getByCondition(" and serialNo=? and type=1 ORDER BY reportDate desc" ,new Object[]{sequence});
                for (int i=0;i<fwWReportCusVisitEntity.size();i++){
                    StringBuffer stringBuffer = new StringBuffer();
                    stringBuffer.append("SELECT * FROM v_com_attach WHERE 1=1" + "\n");
                    stringBuffer.append("AND moduleId=" + " '" +fwWReportCusVisitEntity.get(i).getId()  + " '");
                    List<JSONObject> attaches = wReportCusAndVisitDao.queryForJsonList(stringBuffer.toString(), new Object[]{});
                    if (attaches.size() == 0) {
                        attaches = new ArrayList<JSONObject>();
                    }
                    fwWReportCusVisitEntity.get(i).setAttaches(attaches);
                }

            }
            equipmentAccountEntity.setFwWReportCusVisitEntity(fwWReportCusVisitEntity);
            //索赔记录
            List<WReportCusAndVistEntity> spWReportCusVisitEntity=new ArrayList<>();
            if (!StringUtils.isBlank(sequence)){
                spWReportCusVisitEntity=wReportCusAndVisitDao.getByCondition(" and serialNo=? and type=2 ORDER BY reportDate desc" ,new Object[]{sequence});
                for (int i=0;i<spWReportCusVisitEntity.size();i++){
                    StringBuffer stringBuffer = new StringBuffer();
                    stringBuffer.append("SELECT * FROM v_com_attach WHERE 1=1" + "\n");
                    stringBuffer.append("AND moduleId=" + " '" +spWReportCusVisitEntity.get(i).getId()  + " '");
                    List<JSONObject> attaches = wReportCusAndVisitDao.queryForJsonList(stringBuffer.toString(), new Object[]{});
                    if (attaches.size() == 0) {
                        attaches = new ArrayList<JSONObject>();
                    }
                    spWReportCusVisitEntity.get(i).setAttaches(attaches);
                }
            }
            equipmentAccountEntity.setSpWReportCusVisitEntity(spWReportCusVisitEntity);
            //销售记录
            List<WReportCusAndVistEntity> xsWReportCusVisitEntity=new ArrayList<>();
            if (!StringUtils.isBlank(sequence)){
                xsWReportCusVisitEntity=wReportCusAndVisitDao.getByCondition(" and serialNo=? and type=3 ORDER BY reportDate desc" ,new Object[]{sequence});
                for (int i=0;i<xsWReportCusVisitEntity.size();i++){
                    StringBuffer stringBuffer = new StringBuffer();
                    stringBuffer.append("SELECT * FROM v_com_attach WHERE 1=1" + "\n");
                    stringBuffer.append("AND moduleId=" + " '" +xsWReportCusVisitEntity.get(i).getId()  + " '");
                    List<JSONObject> attaches = wReportCusAndVisitDao.queryForJsonList(stringBuffer.toString(), new Object[]{});
                    if (attaches.size() == 0) {
                        attaches = new ArrayList<JSONObject>();
                    }
                    xsWReportCusVisitEntity.get(i).setAttaches(attaches);
                }

            }
            equipmentAccountEntity.setXsWReportCusVisitEntity(xsWReportCusVisitEntity);
        }
        return (JSONObject)JSONObject.toJSON(equipmentAccountEntity);
    }

    @Override
    public Boolean save(String jsonData) {
        EquipmentAccountEntity equipmentAccountEntity = JSON.toJavaObject(JSON.parseObject(jsonData), EquipmentAccountEntity.class);
        //获取基本信息
        DeviceInfoEntity deviceInfoEntity=equipmentAccountEntity.getDeviceInfo();
        String id=deviceInfoEntity.getId();
        //修改新机表
        //先获取老的信息
        DeviceSampleMainEntity deviceSampleMainEntity=(DeviceSampleMainEntity)deviceSampleDao.getIBaseEntityById(id);
        //对手填的内容进行修改
        if (deviceSampleMainEntity!=null) {
            deviceSampleMainEntity.setCooperationType(deviceInfoEntity.getCooperationType());
            deviceSampleMainEntity.setAdministratorName(deviceInfoEntity.getAdministratorName());
            deviceSampleMainEntity.setAdministratorPhone(deviceInfoEntity.getAdministratorPhone());
            deviceSampleMainEntity.setOperatorName(deviceInfoEntity.getOperatorName());
            deviceSampleMainEntity.setOperatorPhone(deviceInfoEntity.getOperatorPhone());
            deviceSampleMainEntity.setServicePersonnel(deviceInfoEntity.getServicePersonnel());
            deviceSampleMainEntity.setEngineNumber(deviceInfoEntity.getEngineNumber());
            deviceSampleMainEntity.setWarrantyState(deviceInfoEntity.getWarrantyState());
            deviceSampleMainEntity.setDeviceSource(deviceInfoEntity.getDeviceSource());
            deviceSampleMainEntity.setNextServiceDate(deviceInfoEntity.getNextServiceDate());
            deviceSampleDao.update(deviceSampleMainEntity);
        }
        //修改旧机表
        //先获取老的信息
        DeviceDisplaceEntity deviceDisplaceEntity=(DeviceDisplaceEntity)deviceDisplaceDao.getIBaseEntityById(id);
        //对手填的内容进行修改
        if (deviceDisplaceEntity!=null){
        deviceDisplaceEntity.setCooperationType(deviceInfoEntity.getCooperationType());
        deviceDisplaceEntity.setAdministratorName(deviceInfoEntity.getAdministratorName());
        deviceDisplaceEntity.setAdministratorPhone(deviceInfoEntity.getAdministratorPhone());
        deviceDisplaceEntity.setOperatorName(deviceInfoEntity.getOperatorName());
        deviceDisplaceEntity.setOperatorPhone(deviceInfoEntity.getOperatorPhone());
        deviceDisplaceEntity.setServicePersonnel(deviceInfoEntity.getServicePersonnel());
        deviceDisplaceEntity.setEngineNumber(deviceInfoEntity.getEngineNumber());
        deviceDisplaceEntity.setWarrantyState(deviceInfoEntity.getWarrantyState());
        deviceDisplaceEntity.setDeviceSource(deviceInfoEntity.getDeviceSource());
        deviceDisplaceEntity.setNextServiceDate(deviceInfoEntity.getNextServiceDate());
        deviceDisplaceDao.update(deviceDisplaceEntity);
        }
        //获取共用信息
        String tel=deviceInfoEntity.getCustomerTel();
        String name=deviceInfoEntity.getCustomerName();
        String companyName=deviceInfoEntity.getCompanyName();
        String intentionModel=deviceInfoEntity.getModelName();
        String intentionDevice=deviceInfoEntity.getDeviceTypeName();
        String intentionBrand=deviceInfoEntity.getBrandName();
        String companyId=deviceInfoEntity.getCompanyId();
        String sequence=deviceInfoEntity.getSequence();
        WReportCustomerEntity wReportCustomerEntity=wReportCustomerDao.getByTel(tel);
        //修改客户表的客户类型
        if (wReportCustomerEntity!=null){
            wReportCustomerEntity.setAccountCustomerType(deviceInfoEntity.getAccountCustomerType());
            wReportCustomerDao.update(wReportCustomerEntity);
        }
        //添加服务记录
        List<WReportCusAndVistEntity> fwWReportCusAndVistEntity=equipmentAccountEntity.getFwWReportCusVisitEntity();
        if (fwWReportCusAndVistEntity!=null&&fwWReportCusAndVistEntity.size()>0){
            for (int i=0;i<fwWReportCusAndVistEntity.size();i++){
                WReportCusAndVistEntity fw=fwWReportCusAndVistEntity.get(i);
                //更新客户表
                if (!StringUtils.isBlank(fw.getTel())){
                    WReportCustomerEntity wReportCustomerEntity1=wReportCustomerDao.getByTel(fw.getTel());
                    wReportCustomerEntity1.setRepairOrderNumber(fw.getRepairOrderNumber());
                    wReportCustomerEntity1.setOtherVisitType(fw.getOtherVisitType());
                    wReportCustomerEntity1.setFwWorkingCondition(fw.getFwWorkingCondition());
                    wReportCustomerEntity1.setFwWorkingHoursAvg(fw.getFwWorkingHoursAvg());
                    wReportCustomerEntity1.setFwLatestWorkingHours(fw.getFwLatestWorkingHours());
                    wReportCustomerEntity1.setServerCondition(fw.getServerCondition());
                    wReportCustomerEntity1.setAccessoriesChargesCondition(fw.getAccessoriesChargesCondition());
                    wReportCustomerEntity1.setHoursChargesCondition(fw.getHoursChargesCondition());
                    wReportCustomerEntity1.setPrincipal(fw.getPrincipal());
                    wReportCustomerEntity1.setFwState(fw.getFwState());
                    wReportCustomerDao.update(wReportCustomerEntity1);
                }
                //更新客户拜访表
                WReportCusVisitEntity wReportCusVisitEntity=new WReportCusVisitEntity();
                wReportCusVisitEntity.setId(fw.getId());
                wReportCusVisitEntity.setRepairOrderNumber(fw.getRepairOrderNumber());
                wReportCusVisitEntity.setOtherVisitType(fw.getOtherVisitType());
                wReportCusVisitEntity.setFwWorkingCondition(fw.getFwWorkingCondition());
                wReportCusVisitEntity.setFwWorkingHoursAvg(fw.getFwWorkingHoursAvg());
                wReportCusVisitEntity.setFwLatestWorkingHours(fw.getFwLatestWorkingHours());
                wReportCusVisitEntity.setServerCondition(fw.getServerCondition());
                wReportCusVisitEntity.setAccessoriesChargesCondition(fw.getAccessoriesChargesCondition());
                wReportCusVisitEntity.setHoursChargesCondition(fw.getHoursChargesCondition());
                wReportCusVisitEntity.setPrincipal(fw.getPrincipal());
                wReportCusVisitEntity.setFwState(fw.getFwState());
                wReportCusVisitEntity.setAttachName(fw.getAttachName());
                wReportCusVisitEntity.setAttachUrl(fw.getAttachUrl());
                wReportCusVisitEntity.setReportDate(fw.getReportDate());
                wReportCusVisitEntity.setSerialNo(sequence);
                wReportCusVisitEntity.setType("1");
                if (!StringUtils.isBlank(fw.getSerialNo())){
                        wReportCusVisitDao.update(wReportCusVisitEntity);
                }else{
                    //更新客户拜访表
                    wReportCusVisitEntity.setCompanyName(companyName);
                    wReportCusVisitEntity.setCompanyId(companyId);
                    wReportCusVisitEntity.setIntentionModel(intentionModel);
                    wReportCusVisitEntity.setIntentionDevice(intentionDevice);
                    wReportCusVisitEntity.setIntentionBrand(intentionBrand);
                    if (wReportCustomerEntity!=null) {
                        wReportCusVisitEntity.setCustomerId(wReportCustomerEntity.getId());
                    }
                    wReportCusVisitDao.add(wReportCusVisitEntity);
                }
                //获取id
                List<JSONObject> jsonPalams =fwWReportCusAndVistEntity.get(i).getAttaches();
                this.saveAttachs(wReportCusVisitEntity,jsonPalams,"15");


            }
        }

        //添加索赔记录
        List<WReportCusAndVistEntity> spWReportCusAndVistEntity=equipmentAccountEntity.getSpWReportCusVisitEntity();
        if (spWReportCusAndVistEntity!=null&&spWReportCusAndVistEntity.size()>0){
            for (int i=0;i<spWReportCusAndVistEntity.size();i++){
                WReportCusAndVistEntity sp=spWReportCusAndVistEntity.get(i);
                //更新客户表
                if (!StringUtils.isBlank(sp.getTel())) {
                    WReportCustomerEntity wReportCustomerEntity1 = wReportCustomerDao.getByTel(sp.getTel());
                    wReportCustomerEntity1.setRepairOrderNumber(sp.getRepairOrderNumber());
                    wReportCustomerEntity1.setClaimType(sp.getClaimType());
                    wReportCustomerEntity1.setClaimSubmissionDate(sp.getClaimSubmissionDate());
                    wReportCustomerEntity1.setClaimAmount(sp.getClaimAmount());
                    wReportCustomerEntity1.setReplyDate(sp.getReplyDate());
                    wReportCustomerEntity1.setApprovalAmount(sp.getApprovalAmount());
                    wReportCustomerEntity1.setArrivalDate(sp.getArrivalDate());
                    wReportCustomerEntity1.setArrivalAmount(sp.getArrivalAmount());
                    wReportCustomerEntity1.setRefusalAmount(sp.getRefusalAmount());
                    wReportCustomerEntity1.setRefusalReason(sp.getRefusalReason());
                    wReportCustomerEntity1.setOldCondition(sp.getOldCondition());
                    wReportCustomerEntity1.setReturnDate(sp.getReturnDate());
                    wReportCustomerDao.update(wReportCustomerEntity1);
                }
                //更新客户拜访表
                WReportCusVisitEntity wReportCusVisitEntity=new WReportCusVisitEntity();
                wReportCusVisitEntity.setId(sp.getId());
                wReportCusVisitEntity.setRepairOrderNumber(sp.getRepairOrderNumber());
                wReportCusVisitEntity.setClaimType(sp.getClaimType());
                wReportCusVisitEntity.setClaimSubmissionDate(sp.getClaimSubmissionDate());
                wReportCusVisitEntity.setClaimAmount(sp.getClaimAmount());
                wReportCusVisitEntity.setReplyDate(sp.getReplyDate());
                wReportCusVisitEntity.setApprovalAmount(sp.getApprovalAmount());
                wReportCusVisitEntity.setArrivalDate(sp.getArrivalDate());
                wReportCusVisitEntity.setArrivalAmount(sp.getArrivalAmount());
                wReportCusVisitEntity.setRefusalAmount(sp.getRefusalAmount());
                wReportCusVisitEntity.setRefusalReason(sp.getRefusalReason());
                wReportCusVisitEntity.setOldCondition(sp.getOldCondition());
                wReportCusVisitEntity.setReturnDate(sp.getReturnDate());
                wReportCusVisitEntity.setAttachName(sp.getAttachName());
                wReportCusVisitEntity.setAttachUrl(sp.getAttachUrl());
                wReportCusVisitEntity.setReportDate(sp.getReportDate());
                wReportCusVisitEntity.setSerialNo(sequence);
                wReportCusVisitEntity.setType("2");
                if (!StringUtils.isBlank(sp.getSerialNo())){
                    wReportCusVisitDao.update(wReportCusVisitEntity);
                }else{
                    //更新客户拜访表
                    wReportCusVisitEntity.setCompanyName(companyName);
                    wReportCusVisitEntity.setCompanyId(companyId);
                    wReportCusVisitEntity.setIntentionModel(intentionModel);
                    wReportCusVisitEntity.setIntentionDevice(intentionDevice);
                    wReportCusVisitEntity.setIntentionBrand(intentionBrand);
                    if (wReportCustomerEntity!=null) {
                        wReportCusVisitEntity.setCustomerId(wReportCustomerEntity.getId());
                    }
                    wReportCusVisitDao.add(wReportCusVisitEntity);
                }
                List<JSONObject> jsonPalams =spWReportCusAndVistEntity.get(i).getAttaches();
                this.saveAttachs(wReportCusVisitEntity, jsonPalams, "16");

            }
        }
        //销售
        List<WReportCusAndVistEntity> xsWReportCusAndVistEntity=equipmentAccountEntity.getXsWReportCusVisitEntity();
        if (xsWReportCusAndVistEntity!=null&&xsWReportCusAndVistEntity.size()>0){
            for (int i=0;i<xsWReportCusAndVistEntity.size();i++){
                WReportCusAndVistEntity xs=xsWReportCusAndVistEntity.get(i);
                //更新客户表
                if (!StringUtils.isBlank(xs.getTel())) {
                    WReportCustomerEntity wReportCustomerEntity1 = wReportCustomerDao.getByTel(xs.getTel());
                    wReportCustomerEntity1.setRepairOrderNumber(xs.getRepairOrderNumber());
                    wReportCustomerEntity1.setNumberDelivery(xs.getNumberDelivery());
                    wReportCustomerEntity1.setOutboundAmount(xs.getOutboundAmount());
                    wReportCustomerEntity1.setPaymentAmount(xs.getPaymentAmount());
                    wReportCustomerEntity1.setAmountOwed(xs.getAmountOwed());
                    wReportCustomerEntity1.setEstimatedRepaymentDate(xs.getEstimatedRepaymentDate());
                    wReportCustomerEntity1.setPrincipal(xs.getPrincipal());
                    wReportCustomerEntity1.setActualRepaymentDate(xs.getActualRepaymentDate());
                    wReportCustomerEntity1.setDescription(xs.getDescription());
                    wReportCustomerDao.update(wReportCustomerEntity1);
                }
                //更新客户拜访表
                WReportCusVisitEntity wReportCusVisitEntity=new WReportCusVisitEntity();
                wReportCusVisitEntity.setId(xs.getId());
                wReportCusVisitEntity.setRepairOrderNumber(xs.getRepairOrderNumber());
                wReportCusVisitEntity.setNumberDelivery(xs.getNumberDelivery());
                wReportCusVisitEntity.setOutboundAmount(xs.getOutboundAmount());
                wReportCusVisitEntity.setPaymentAmount(xs.getPaymentAmount());
                wReportCusVisitEntity.setAmountOwed(xs.getAmountOwed());
                wReportCusVisitEntity.setEstimatedRepaymentDate(xs.getEstimatedRepaymentDate());
                wReportCusVisitEntity.setPrincipal(xs.getPrincipal());
                wReportCusVisitEntity.setActualRepaymentDate(xs.getActualRepaymentDate());
                wReportCusVisitEntity.setDescription(xs.getDescription());
                wReportCusVisitEntity.setAttachName(xs.getAttachName());
                wReportCusVisitEntity.setAttachUrl(xs.getAttachUrl());
                wReportCusVisitEntity.setReportDate(xs.getReportDate());
                wReportCusVisitEntity.setSerialNo(sequence);
                wReportCusVisitEntity.setType("3");
                if (!StringUtils.isBlank(xs.getSerialNo())){
                    wReportCusVisitDao.update(wReportCusVisitEntity);
                }else{
                    //更新客户拜访表
                    wReportCusVisitEntity.setCompanyName(companyName);
                    wReportCusVisitEntity.setCompanyId(companyId);
                    wReportCusVisitEntity.setIntentionModel(intentionModel);
                    wReportCusVisitEntity.setIntentionDevice(intentionDevice);
                    wReportCusVisitEntity.setIntentionBrand(intentionBrand);
                    if (wReportCustomerEntity!=null) {
                        wReportCusVisitEntity.setCustomerId(wReportCustomerEntity.getId());
                    }
                    wReportCusVisitDao.add(wReportCusVisitEntity);
                }
                List<JSONObject> jsonPalams =xsWReportCusAndVistEntity.get(i).getAttaches();
                this.saveAttachs(wReportCusVisitEntity, jsonPalams, "17");

            }

        }
        return true;
    }
    /*
     * 保存对应的附件
     * */
    public void saveAttachs(WReportCusVisitEntity wReportCusVisitEntity, List<JSONObject> attachModuleList, String module) {
        //删除现有的附件模块表中的信息
        attachModuleDao.removeByCondition("and MODULE_ID=? and ATTACH_MODULE=? ", new String[]{wReportCusVisitEntity.getId(), module});
        //获取上传附件的信息
        if (!org.apache.commons.lang3.StringUtils.isBlank(wReportCusVisitEntity.getId()) && attachModuleList.size() > 0) {
            for (JSONObject attachModule : attachModuleList) {
                //附件信息写入附件-模板表中
                AttachmentEntity attachmentEntity = JSON.toJavaObject(attachModule, AttachmentEntity.class);
                AttachModuleEntity attachModuleEntity = new AttachModuleEntity();
                attachModuleEntity.setAttachId(attachmentEntity.getId());
                attachModuleEntity.setAttachModule(Integer.parseInt(module));
                attachModuleEntity.setModuleId(wReportCusVisitEntity.getId());
                attachModuleDao.add(attachModuleEntity);
            }
        }
    }

}
